import {listTasks} from '@/api/xcgl';
import {add, remove, update, list, listByGeo} from '@/api/xcgl/ysbj'


const amap = {
  namespaced:true,
  state: {
    measureSync: undefined,                     //同步测量
    comparable: undefined,                      //双屏对比
    mapService:undefined,                       //地图服务类
    mapServiceLeft:undefined,
    mapServiceRight:undefined,
    aspect:undefined,                           //地图切片
    featureCollection: [],                      //当前显示服务要素
    featureService: [],                         //要素服务集合
    featuresCache: {},                          //要素服务缓存
    selectTask: undefined,                      //当前选中的服务
    drawControl: undefined,                     //drawControl组件
    editTableVisible: undefined,                //编辑属性表的显隐
    featureFieldsCache: {},                     //服务字段属性缓存
    featureFields: undefined,                   //要素服务的属性集合
    selectSymbol: undefined,                    //选择符号
    layerSelectorVisible:undefined,             //图层选择器是否可见
    currentSearchLayer: undefined,
    attributeTable: undefined,
    attributeTableData: [],
    isShowAttribute:false,
    propertiesShow:false
  },

  mutations: {
    setMeasureSync: (state, m) => {
      state.measureSync = m;
    },
    setComparable: (state, c) => {
      state.comparable = c;
    },
    setFeatureCollection: (state, features) => {
      state.featureCollection = features;
    },
    setFeatureService: (state, featureservice) => {
      state.featureService = featureservice;
    },
    setSelectTask: (state, task) => {
      state.selectTask = task;
    },
    setDrawControl: (state, drawControl) => {
      state.drawControl = drawControl;
    },
    setEditTableVisible: (state, isVisible) => {
      state.editTableVisible = isVisible;
    },
    setFeaturesCache: (state, cache) => {
      state.featuresCache = cache;
    },
    setFeatureFields: (state, cache) => {
      state.featureFields = cache;
    },
  
    setSelectSymbol: (state, symbol) => {
      state.selectSymbol = symbol;
    },
    setLayerSelectorVisible:(state,visible)=>{
      state.layerSelectorVisible = visible;
    },
    setMapService:(state,mapService)=>{
      state.mapService = mapService;
    },
    setMapServiceLeft:(state,mapService)=>{
      state.mapServiceLeft = mapService;
    },
    setMapServiceRight:(state,mapService)=>{
      state.mapServiceRight = mapService;
    },
    SET_ASPECT:(state,aspect)=>{
      state.aspect = aspect;
    },
    setCurrentSearchLayer: (state, currentSearchLayer) => {
      state.currentSearchLayer = currentSearchLayer
    },
    setAttributeTable: (state, isTrue) => {
      state.attributeTable = isTrue
    },
    setAttributeTableData: (state, data) => {
      state.attributeTableData = data
    },
    setIsShowAttribute:(state,show)=>{
      state.isShowAttribute = show;
    },
    //属性显示
    setProertiesShow:(state,show)=>{
      state.propertiesShow = show;
    }
  },

  actions: {
   /**
     * 图层查询属性
     * @param commit
     * @param state
     * @returns {Promise<any>}
     * @constructor
     */
    QueryLayerAttributes({ commit, state }, param) {
      return new Promise((resolve, reject) => {
           state.mapServiceRight.queryLayerAttribute(param.layer, param.start, param.size, param.sql ? param.sql : '1=1').then(res=>{
            commit('setAttributeTableData', res.data)
            resolve(res)
          }).catch(e=>{
            reject(e);
          })
        
      })
    },
    /**
     * 获取每个用户的任务
     * @param commit
     * @param state
     * @param param
     * @returns {Promise<any>}
     * @constructor
     */
    RequestTasksUnderUser({commit, state}, param) {
      return new Promise((resolve, reject) => {
        listTasks({uid: this.getters.id, dept: this.getters.dept}).then(res => {
          commit('setFeatureService', res.data);  //提交任务
          resolve(res.data);
        }).catch(e => {
          reject(e);
        })
      });
    },

    /**
     * 点击任务
     * 1、显示属性框
     * 2、显示属性
     * 3、请求数据并缓存
     * 4、加载符号
     * 5、实例化drawControl
     *
     * @param commit
     * @param state
     * @param param
     * @returns {Promise<any>}
     * @constructor
     */
    RequestFeatureFromTask({commit, state}, param) {
      return new Promise((resolve, reject) => {
        commit('setSelectTask', param);
        commit('setEditTableVisible', true);
        commit('setFeatureCollection', []);
        //初始化DrawControl
       const left=  state.mapServiceLeft.createDrawControl(param.symbol);
       const right =  state.mapServiceRight.createDrawControl(param.symbol);
       state.comparable.setDrawControl(left,right);
        list({
          dept: this.getters.dept,
          taskId: state.selectTask._id
        }).then(res => {
          commit('setFeatureCollection', res.data); //提交要素集合
          state.featuresCache[param._id] = res.data;//缓存数据
          state.mapServiceLeft.setDrawControlFC({type: 'FeatureCollection', features: res.data});
          state.mapServiceRight.setDrawControlFC({type: 'FeatureCollection', features: res.data});
          resolve(res.data);
        }).catch(e => {
          reject(e);
        })
      });
    },

  }

}

export default amap
